#!/usr/bin/env bash
#---------HEADER--------------------------------------------------------------#
#AUTOR
# Jefferson Rocha 'Slackjeff' <lrcjefferson@gmail.com>
#
#PROGRAMA
# slackdesc - generate tool slack-desc
#
#LICENÇA
# MIT - https://notabug.org/jeffersonrocha/slackdesc/raw/master/LICENSE
#
#CHANGELOG
#  0.1b - Jefferson Rocha 07/04/2018
#	-added interactive mode
#-----------------------------------------------------------------------------#



#---VARIABLES--------------#
version="0.1b"
red="\033[31;1m"
yellow="\033[33;1m"
end="\033[m"


#----FUNCTIONS-------------#
# Usage Options
_usage(){
echo -e "\n${red}Usage:${end} slackdesc [OPTION] [PACKAGE-NAME]"
echo "To create slack-desc enter the package's main directory, "install" and run 
slackdesc."
echo -e "\n${red}Options:${end}"
echo -e "\t -c, --create (For generate slack-desc template with name of package.)\n"
echo -e "\t -i, --interactive (Questionnaire mode, asks the package name,\n\t small description and homepage of the project.\n"
echo -e "\t -v, --version (Look version)\n"
}




# Create slack-desc in mode (-c or --create)
_create(){
	cat > slack-desc <<-EOF
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.  Line
# up the first '|' above the ':' following the base package name, and the '|' on
# the right side marks the last column you can put a character in.  You must make
# exactly 11 lines for the formatting to be correct.  It's also customary to
# leave one space after the ':'.

        |-----handy-ruler------------------------------------------------------|
$appname: $appname (Short description of the application)
$appname:
$appname:
$appname:
$appname:
$appname:
$appname:
$appname:
$appname:
$appname: homepage
$appname:

	EOF
}

# Ask mode (-i or --interactive)
_interactive(){
	clear	

	while :; do
		read -p $'\033[33;1mName Package: \033[m' appname
		[[ -z "$appname" ]] && { echo "Need name package! no use spaces, only -" ; exit 1 ;}

		read -en 50 -p $'\033[33;1mVery Short Description, dont ultrapass 50 characters: \033[m \n' short_desc
		sleep 1s
		[[ -z "$short_desc" ]] && { echo "Need short description!" ; exit 1 ;}
	
		read -p $'\033[33;1mHomepage of project: \033[m' homepage

		echo -e "\n------------------------------------------------------------------------"
		echo "Name Package: $appname"
		echo "Short Description: $short_desc"
		echo "Homepage Project: $homepage"
		echo -e "------------------------------------------------------------------------\n"
		read -p "Confirm informations?: [s/n]: " confirm
		confirm=${confirm,,} 	# All in lower
		if [[ "$confirm" != "s" ]]; then
			exit 0
		else
			break
		fi
	done

	cat > slack-desc <<-EOF
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.  Line
# up the first '|' above the ':' following the base package name, and the '|' on
# the right side marks the last column you can put a character in.  You must make
# exactly 11 lines for the formatting to be correct.  It's also customary to
# leave one space after the ':'.

        |-----handy-ruler------------------------------------------------------|
$appname: $appname ($short_desc)
$appname:
$appname:
$appname:
$appname:
$appname:
$appname:
$appname:
$appname:
$appname: $homepage
$appname:
	EOF

[[ -f "slack-desc" ]] && echo -e "slack-desc was successfully created in the current directory!"
}


#----START HERE---------#
case $1 in
	-c|--create)
  		shift
  		[[ -z "$1" ]] && { echo "Name program???, use slackdesc --help for all options." ;exit 1 ;}
  		appname="$1"
  		_create # Call function
	;;

	-i|--interactive)
		_interactive # Call Function
	;;

	-v|--version) echo "slackdesc $version" ;;
	-h|--help) _usage ;;
 
	*) _usage ;;
esac


